//
// Created by win on 2017/2/21.
//
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"
#include "../../../src/module/thrift/config.h"
#include "../../../src/module/thrift/tcompact/tcompact.h"
#include "../../../src/module/thrift/thrift_core.h"

#include "apmtest_cunit.h"
#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;

    //测试修改部分
    i_ret = frthrift_tcompact_read_double_begin(p_st_data, p_st_body_data, NULL, FR_YES, (char *)"request.thrift");

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);
    frreport_body_free(&p_st_body_data);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    FRTcompact st_compact = {0, 0, 0, 0};
    i_ret = apmtest_create_ct(&p_st_conn, thrift_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 9090, 5672);
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    //测试修改部分
    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,(char *)"\x9a\x99\x99\x99\x99\x59\x3e\x40", 8);

    i_ret = frthrift_tcompact_read_double_begin(p_st_data, p_st_body_data, &st_compact, FR_YES, (char *)"request.thrift");

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    bytebuff_free(&p_st_data);
    frreport_body_free(&p_st_body_data);
    apmtest_destroy_ct(&p_st_conn);

    return;
}

static void case3(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;
    FRTcompact st_compact = {0, 0, 0, 0};
    i_ret = apmtest_create_ct(&p_st_conn, thrift_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 9090, 5672);
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    //测试修改部分
    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,(char *)"\x9a\x99\x99\x99\x99\x59\x3e", 7);

    i_ret = frthrift_tcompact_read_double_begin(p_st_data, p_st_body_data, &st_compact, FR_YES, (char *)"request.thrift");

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);
    frreport_body_free(&p_st_body_data);
    apmtest_destroy_ct(&p_st_conn);

    return;
}

int add_suite_frthrift_tcompact_read_double_begin(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" frthrift_tcompact_read_double_begin()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);

    return FR_SUCC;

}